﻿using System.Threading.Tasks;
using System;

namespace Client.Panels
{
    public class GGRankingPanel : BasePanel
    {
        protected override string[] parentsName => new string[] { "BasePanel" };
    
        string bkSymbol;
        int companyNum = 0;
        int numPerPage = 15;
        public int pageNum
        {
            get
            {
                if (companyNum % numPerPage == 0)
                    return companyNum / numPerPage;
                return companyNum / numPerPage + 1;
            }
        }
        public int currentPage { get; private set; }
        bool des = false;
        DataCenter.Enums.SortFlag sortFlag = DataCenter.Enums.SortFlag.changepercent;

        bool canChangePage = true;
        int limitChangePageMS = 2000;

        public GGRankingPanel(string bkSymbol,string clientText):base(clientText)
        {
            this.bkSymbol = bkSymbol;
            currentPage = 1;
            RegisterHotkey(panelName);
        }

        /// <summary>
        /// 初始化公司数量，需要在创建实例后await调用此方法在添加到panelManager
        /// </summary>
        /// <returns></returns>
        public async Task<int> Init()
        {
            companyNum = await DataCenter.DataCenter.GetStockNumInBK(bkSymbol);
            FillFields<DataCenter.Models.StockInfo>(panelName);
            return 0;
        }

        async Task<int> GetList()
        {
            try
            {
                string[] ggl = await DataCenter.DataCenter.GetStockListOfBK(bkSymbol, currentPage, sortFlag, des, numPerPage);
                Managers.Manager.cronM.UpdateGGSymbol(ggl);
            }
            catch (DataCenter.MyException.APIReturnException e)
            {
                var l = e.Message.Split('~');
                if (l[0] == "1000")
                    Utility.ShowLoginAgain();
                throw;
            }
            return 0;
        }
        protected override void UpdatePanel()
        {
            UpdatePanel(Managers.Manager.cronM.ggStocks);
        }
        public override void PageDown()
        {
            if (canChangePage && currentPage < pageNum)
            {
                currentPage++;
                Managers.Manager.cronM.StartCron(true);
                InitIndex();
                LimitChangePage();
            }
        }
        public override void PageUp()
        {
            if (canChangePage && currentPage > 1)
            {
                currentPage--;
                Managers.Manager.cronM.StartCron(true);
                InitIndex();
                LimitChangePage();
            }
        }
        public override void Enter()
        {
            var p = new TickLinePanel(Managers.Manager.cronM.ggStocks[y]);
            Managers.Manager.panelM.AddPanel(p);
        }

        public override void OnSleep()
        {
            base.OnSleep();
            Managers.Manager.cronM.beginCron -= GetList;
            Managers.Manager.cronM.updateSuccessfully -= UpdatePanel;
            Managers.Manager.hotStockInfo = null;
        }
        protected override void ClearCron()
        {
            Managers.Manager.cronM.ClearGGSymbols();
        }
        public override void OnAwake()
        {
            base.OnAwake();
            Managers.Manager.cronM.updateSuccessfully += UpdatePanel;
            Managers.Manager.cronM.beginCron += GetList;
            Managers.Manager.cronM.StartCron(true);
        }

        protected override void UpdateHotStockInfo()
        {
            if (y > -1)
                StockMenuFuncs.UpdateHotStockInfo(Managers.Manager.cronM.ggStocks[y]);
        }

        async void LimitChangePage()
        {
            canChangePage = false;
            await Task.Delay(limitChangePageMS);
            canChangePage = true;
        }

        protected override string GetYPositionString()
        {
            int i = (currentPage - 1) * numPerPage + y + 1;
            var str = companyNum + "之" + i + "项";
            return str;
        }
    }
}
